from fastapi import APIRouter, Depends, Body
from dependencies import get_mongo_db_of_condition_safety
import motor.motor_asyncio
from schema.req.condition_safety_settings import *
from schema.resp import RestfulModel
from typing import List
from pymongo.database import Database

AsyncIOMotorCollection = motor.motor_asyncio.AsyncIOMotorCollection


condition_safety_settings_router = APIRouter(prefix='/condition_safety/setting', tags=['车辆状态安全-配置项'])


class SettingsName:
    CAR_MAP = 'Car_map'
    VEHICLE_TYPE = 'Vehicle_type_statistics'
    VEHICLE_POWER_CONSUMPTION = 'Abnormal_vehicle_power_consumption'
    VEHICLE_CURRENT = 'Abnormal_vehicle_current'
    ACCELERATOR_PEDAL = 'Abnormal_accelerator_pedal'
    BRAKE_PEDAL = 'Abnormal_brake_pedal'
    SOC_ABNORMALITY = 'SOC_abnormality'
    STATUS_STATISTICS = 'Abnormal_state_count'
    ATTACK_DISPLAY = 'Attack_situation_display'
    ATTACK_STATISTICS = 'Attack_Statistics'
    SECURITY_LOG = 'security-log'



success_resp = RestfulModel.success(None)
@condition_safety_settings_router.get('/init-settings',
                                     response_model=RestfulModel,
                                     summary='配置项初始化')
async def get_init_settings(mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    name_list={
        "Abnormal_vehicle_current":'总电流异常',
        "Abnormal_accelerator_pedal":"加速踏板异常",
        "Abnormal_brake_pedal":"制动踏板异常",
        "SOC_abnormality":"SOC异常",
        "Car_map":"车辆地图",
        "Abnormal_vehicle_power_consumption":"车辆耗电异常",
        "Vehicle_type_statistics":"车型接口",
        "Abnormal_state_count":"异常状态统计",
        "Attack_situation_display":"攻击态势展示",
        "Attack_Statistics":"车辆攻击统计",
        "security-log": "安全日志 / 告警日志监控日期"
    }
    result={}
    for name in name_list:
        result[name]={
            'zh_name':name_list[name],
            'data':coll.find({'name':name}, {'_id': 0})[0]['data']
        }

    return RestfulModel.success(result)



@condition_safety_settings_router.post('/map_info',
                                     response_model=RestfulModel,
                                     summary='地图信息的配置')
async def modify_map_info_settings(settings: MapInfoSettings = Body(...),
                                   mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. CAR_MAP}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/car_type',
                                     response_model=RestfulModel,
                                     summary='车型接口的配置')
async def modify_car_type_settings(settings: CarTypeSettings = Body(...),
                                      mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. VEHICLE_TYPE}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/car_electron',
                                     response_model=RestfulModel,
                                     summary='车辆耗电异常展示的配置')
async def modify_car_electron_settings(settings: CarElectronSettings = Body(...),
                                        mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. VEHICLE_POWER_CONSUMPTION}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/car_electron_stream',
                                     response_model=RestfulModel,
                                     summary='车辆电流异常展示的配置')
async def modify_car_electron_stream_settings(settings: VehicleCurrentSettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. VEHICLE_CURRENT}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/speed_exce',
                                     response_model=RestfulModel,
                                     summary='加速踏板异常展示的配置')
async def modify_speed_exce_settings(settings: AcceleratorPedalSettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. ACCELERATOR_PEDAL}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/stop_exce',
                                     response_model=RestfulModel,
                                     summary='制动踏板异常展示的配置')
async def modify_stop_exce_settings(settings: BreakPedalSettings = Body(...),
                                             mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. BRAKE_PEDAL}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/soc_exce',
                                     response_model=RestfulModel,
                                     summary='SOC异常展示的配置')
async def modify_soc_exce_settings(settings: SocAbnormalitySettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. SOC_ABNORMALITY}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/exce_status',
                                     response_model=RestfulModel,
                                     summary='异常状态统计的配置')
async def modify_exce_status_settings(settings: StatusStatisticsSettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. STATUS_STATISTICS}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/attack_show',
                                     response_model=RestfulModel,
                                     summary='攻击态势展示的配置')
async def modify_attack_show_settings(settings: AttackDisplaySettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. ATTACK_DISPLAY}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/car_attack',
                                     response_model=RestfulModel,
                                     summary='车辆攻击统计的配置')
async def modify_car_attack_settings(settings: AttackStatisticsSettings = Body(...),
                                         mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll=mongo_db['settings']
    coll.update_one({'name': SettingsName. ATTACK_STATISTICS}, {'$set': {'data': settings.dict()}})
    return success_resp


@condition_safety_settings_router.post('/security-log',
                                       response_model=RestfulModel,
                                       summary='修改安全日志/告警日志的配置')
async def modify_security_log_settings(settings: SecurityLogSettings = Body(...),
                                       mongo_db: Database = Depends(get_mongo_db_of_condition_safety)):
    coll = mongo_db['settings']
    coll.update_one({'name': SettingsName.SECURITY_LOG}, {'$set': {'data': settings.dict()}})
    return success_resp


#车型统计类型选择（要不车型直接输入）
@condition_safety_settings_router.get('/car_type',
                                    response_model=RestfulModel[List],
                                    summary='获取车型，用于供前端选择类型')
async def get_violation_types():
    return RestfulModel.success(["LDP31B96", "LGJE13EA", "LDP95E96","青岛885340"])


#异常状态统计类型选择
@condition_safety_settings_router.get('/exce_status',
                                    response_model=RestfulModel[List],
                                    summary='获取异常状态统计的异常类型，用于供前端选择类型')
async def get_violation_types():
    return RestfulModel.success([
            "Abnormal_vehicle_current",
			"Abnormal_accelerator_pedal",
			"Abnormal_brake_pedal",
			"SOC_abnormality",
			"Abnormal_vehicle_power_consumption"])


#攻击态势展示类型选择
@condition_safety_settings_router.get('/attack_show',
                                    response_model=RestfulModel[List],
                                    summary='获取攻击态势展示的攻击类型，用于供前端选择类型')
async def get_violation_types():
    return RestfulModel.success(["HTTP","Dos","port scan","blocked ip"])


#车辆攻击统计类型选择
@condition_safety_settings_router.get('/car_attack',
                                    response_model=RestfulModel[List],
                                    summary='获取车辆攻击统计的攻击类型，用于供前端选择类型')
async def get_violation_types():
    return RestfulModel.success([
			"HTTP_sql insertion",
			"HTTP_Reflect XSS",
			"HTTP_Stored XSS",
			"HTTP_Host not match",
            "HTTP_sensitive data",
			"Dos_Dos from single host",
			"port scan_TCP SYN Scan",
			"port scan_UDP Scan",
            "blocked ip_ip incoming",
            "blocked ip_ip outgoing"])









